package com.zynn.service.module.calculate.push.scheduler;

import com.zynn.common.core.config.redis.RedisLockConstant;
import com.zynn.message.queue.constant.MqQueueNameConstant;
import com.zynn.message.queue.service.SenderService;
import com.zynn.service.module.calculate.service.EventsService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import redis.clients.jedis.JedisCluster;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 *@ClassName PersonGroupActionScheduler
 *@Description TODO
 *@Author 刘猛
 *@Date 2018/11/5 15:53
 **/

@Slf4j
@Component
@Configuration
@EnableScheduling
public class PersonGroupActionPushScheduler {

    @Autowired
    JedisCluster jedisCluster;

    @Autowired
    private SenderService senderService;

    @Autowired
    EventsService eventsService;

    /**
     * 相册内有新上传 一次以上21:30推送
     */
    @Scheduled(cron = "0 30 21 * * ? ")
    public void personGroupUploadPush(){
        log.info("用户好友推荐开始,推送开始");
        Long lock = jedisCluster.setnx(RedisLockConstant.PERSON_GROUP_EVENT_PUSH_LOCK, "lock");
        if (!lock.equals(1L)){
            return;

        }
        /**
         * 私密相册内有1次以上照片新增
         */
        try {
            List<Map<String,Object>> groupsMapList = eventsService.getEventOnceUpGroups();
            if(CollectionUtils.isEmpty(groupsMapList)){
                return;
            }
            groupsMapList.forEach(e -> {
                senderService.send(MqQueueNameConstant.PERSON_GROUP_BATCH_UPLOAD_PUSH,e);
            });
        }catch (Exception e){

        }finally {
            jedisCluster.del(RedisLockConstant.PERSON_GROUP_EVENT_PUSH_LOCK);
            eventsService.deleteGroupPushCacheDataToday();
        }

    }

    /**
     * 被点赞 超过3次的每晚20:00推送
     */
    @Scheduled(cron = " 0 0 20 * * * ")
    public void beCommentPush(){
        log.info("用户好友推荐开始,推送开始");
        Long lock = jedisCluster.setnx(RedisLockConstant.PERSON_GROUP_LIKE_PUSH_LOCK, "lock");
        if (!lock.equals(1L)){
            return;

        }
        /**
         * 私密相册内3次以上点赞
         */
        try {
            List<Map<String,Object>> groupsMapList = eventsService.likesUpThree();
            if(CollectionUtils.isEmpty(groupsMapList)){
                return;
            }
            groupsMapList.forEach(e -> {
                senderService.send(MqQueueNameConstant.BE_LIKED_BATCH_PUSH,e);
            });
        }catch (Exception e){

        }finally {
            jedisCluster.del(RedisLockConstant.PERSON_GROUP_LIKE_PUSH_LOCK);
        }
    }


    /**
     * 被点赞 超过3次的每晚20:00推送  SNS首页
     */
//    @Scheduled(cron = " 0 0 20 * * * ")
    public void beLikeSnsPush(){
        log.info("用户好友推荐开始,推送开始");
        Long lock = jedisCluster.setnx(RedisLockConstant.SNS_LIKE_PUSH_LOCK, "lock");
        if (!lock.equals(1L)){
            return;
        }
        /**
         * 私密相册内3次以上点赞
         */
        try {
            List<Map<String,Object>> groupsMapList = eventsService.snsLikesUpThree();
            if(CollectionUtils.isEmpty(groupsMapList)){
                return;
            }
            groupsMapList.forEach(e -> {
                senderService.send(MqQueueNameConstant.BE_LIKED_BATCH_PUSH,e);
            });
        }catch (Exception e){

        }finally {
            jedisCluster.del(RedisLockConstant.SNS_LIKE_PUSH_LOCK);
        }
    }


    /**
     * 新创建空相册，未传照片
     */
    @Scheduled(cron = " 0 0 22 * * * ")
    public void newEmptyGroupPush(){
        log.info("用户好友推荐开始,推送开始");
        Long lock = jedisCluster.setnx(RedisLockConstant.NEW_EMPTY_GROUP_PUSH_LOCK, "lock");
        if (!lock.equals(1L)){
            return;

        }
        /**
         * 私密相册内3次以上点赞
         */
        try {
            List<Map<String,Object>> userIds = eventsService.newEmptyGroup();
            if(CollectionUtils.isEmpty(userIds)){
                return;
            }
            userIds.forEach(e -> {
                senderService.send(MqQueueNameConstant.NEW_EMPTY_GROUP_BATCH_PUSH,e);
            });
        }catch (Exception e){

        }finally {
            jedisCluster.del(RedisLockConstant.NEW_EMPTY_GROUP_PUSH_LOCK);
        }
    }
}
